<?php
/****************************************************************
*move up and move down the page order
*****************************************************************/

include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../site_specific/external_constants.php";
include "../../includes/phpmailer/class.phpmailer.php";
include "../../includes/phpmailer/class.smtp.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/date.php";
include "../../includes/text.php";
include "../../includes/files.php";

# Establish database connection
establishDatabaseConnection();

$sql_all_parents = "SELECT distinct p_parent FROM ".$PROJECT_DB_TABLES['pages'];

$current_data_all_parents = getRecord($sql_all_parents);

$sql_module_level = "SELECT p_id FROM ".$PROJECT_DB_TABLES['pages']." WHERE ";

$count = 0;

foreach($current_data_all_parents as $key_all_parents=>$value_all_parents){
	
	$sql_module_level .= "p_id!=\"".$value_all_parents["p_parent"]."\"";
	
	if(count($current_data_all_parents) != $count+1){
		
		$sql_module_level .= " AND ";
	}
	
	$count++;
}

$current_data_module_level = getRecord($sql_module_level);

#Get the id of moved item

$id = $_GET["id"];

#if action is up,it means the moved item is moving up otherwise is moving down.

$action = $_GET["action"];

$parent = $_GET["parent"];

$section = $_GET["section"];

$sql_refreshed_items = "SELECT p_id,p_name,p_parent,p_status,p_sort FROM ".$PROJECT_DB_TABLES['pages']." where p_parent=\"".$parent."\" AND p_section=\"".$section."\" AND p_status>0 AND p_status<3 order by p_sort";

$current_data_items = getRecord($sql_refreshed_items);

$sql_moved_item = "SELECT p_id,p_name,p_parent,p_status,p_sort FROM ".$PROJECT_DB_TABLES['pages']." where p_id=\"".$id."\" AND p_status>0 AND p_status<3";

$current_moved_item = getRecord($sql_moved_item);

$selectedID = $id;

$selectedsort = $current_moved_item[0]["p_sort"];

$relpacedID = "";

$relpacedsort = 0;
	
if($action=="up"){	

	$topflag=true;
		
	$orderActual = findInPagesArray($current_moved_item[0],$current_data_items);
	
	if($orderActual == 1){		
		
		$topflag = false;
	}
	else{
		
		$orderReplacedItem = $orderActual - 2;
		
		$relpacedID = $current_data_items[$orderReplacedItem]["p_id"];
		
		$selectedID = $current_data_items[$orderActual-1]["p_id"];
		
		$relpacedsort = $orderActual;
		
		$selectedsort = $orderActual-1;
		
		echo "orderActual:".$orderActual."<br/>";
	}
	
	if($topflag != false){

		$sql_update_replaced="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$relpacedsort." WHERE p_id=\"".$relpacedID."\"";
		
		executeSQLQuery($sql_update_replaced);
		
		$sql_update_selected="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$selectedsort." WHERE p_id=\"".$selectedID."\"";
		
		executeSQLQuery($sql_update_selected);

		
		//echo $sql_update_replaced."____".$sql_update_selected;
	}
}
else if($action=="down"){
	
	$bottomflag=true;
		
	$orderActual = findInPagesArray($current_moved_item[0],$current_data_items);

	//echo $orderActual."==".count($current_data_items[0])."<br/>";
	
	if($orderActual == count($current_data_items)){		
		
		$bottomflag = false;
	}
	else{
		
		$orderReplacedItem = $orderActual;
		
		$relpacedID = $current_data_items[$orderReplacedItem]["p_id"];
		
		$selectedID = $current_data_items[$orderActual-1]["p_id"];
		
		$relpacedsort = $orderActual ;
		
		$selectedsort = $orderActual+1;
		
		//echo "orderActual:".$orderActual."<br/>";
	}
	
	if($bottomflag != false){

		$sql_update_replaced="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$relpacedsort." WHERE p_id=\"".$relpacedID."\"";
		
		executeSQLQuery($sql_update_replaced);
		
		$sql_update_selected="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$selectedsort." WHERE p_id=\"".$selectedID."\"";
		
		executeSQLQuery($sql_update_selected);

		
		//echo $sql_update_replaced."____".$sql_update_selected;
	}
}

$output.="<table cellspacing=\"0\" align=\"left\">";

$sql_refreshed_items="SELECT * FROM ".$PROJECT_DB_TABLES['pages']." where p_parent=\"".$parent."\" AND p_section=\"".$section."\" AND p_status>0 AND p_status<3 order by p_sort";

$current_data_items=getRecord($sql_refreshed_items);

foreach($current_data_items as $current_key=>$current_value){
	
	$module_flag=false;
	foreach($current_data_module_level as $value_current_data_module_level){
		
		if($current_value["p_id"]==$value_current_data_module_level["p_id"]){
			
			$module_flag=true;
			
			break;
		}
	}
	if($module_flag=="true"){
		
		$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/document.png\");\"/></td>";
		
		$output.="<td style=\"border-color: white;border-style: solid;\" align=\"left\"><a id=\"a_".$current_value["p_id"]."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$current_value["p_id"]."','".$current_value["p_parent"]."','".$current_value["p_section"]."');\" style=\"text-decoration:none;\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";
	}
	else{
		
		$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandPage('".$current_value["p_id"]."',false,true);\"/></td><td style=\"border-color: white;border-style: solid;\" align=\"left\"><a id=\"a_".$current_value["p_id"]."\" onclick=\"\" href=\"javascript:itemSelected('".$current_value["p_id"]."','".$current_value["p_parent"]."','".$current_value["p_section"]."');\" style=\"text-decoration:none;\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";		
	}
}

$output.="</table>";

echo $output;

/*****************************************
*get the order of current page in page groups
*return $order
****************************************/
function findInPagesArray($page,$pageGroup){
	
	echo "<br/>".$page['p_id']."<br/>";
	
	$order = 1;
	
	foreach($pageGroup as $groupItem){
		
		if($page['p_id'] == $groupItem['p_id']){
			
			return $order;
		}
		
		$order ++;
	}
	
	return false;
}

?>